#include <Qt/qevent.h>
#include <math.h>

#include "glwidget.moc"

GLWidget::GLWidget(QWidget *parent)
    : QGLWidget(parent)
{
		disType = deflt;

		clsToDisplay = 0;
		itrtToDisplay = 0;
    object = 0;

    xRot = 1;
    yRot = 1;
    zRot = 1;

    //trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
    trolltechGreen = QColor::fromCmykF(0.0, 0.0, 0.0, 0.0);
    trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
}
//! [0]

//! [1]
GLWidget::~GLWidget()
{
    makeCurrent();
    glDeleteLists(object, 1);
}
//! [1]

//! [2]
QSize GLWidget::minimumSizeHint() const
{
    return QSize(650, 650);
}
//! [2]

//! [3]
QSize GLWidget::sizeHint() const
//! [3] //! [4]
{
    return QSize(700, 500);
}
//! [4]

//! [5]
void GLWidget::setXRotation(int angle)
{
    normalizeAngle(&angle);
    if (angle != xRot) {
        xRot = angle;
        emit xRotationChanged(angle);
        updateGL();
    }
}
//! [5]

void GLWidget::setYRotation(int angle)
{
    normalizeAngle(&angle);
    if (angle != yRot) {
        yRot = angle;
        emit yRotationChanged(angle);
        updateGL();
    }
}

void GLWidget::setZRotation(int angle)
{
    normalizeAngle(&angle);
    if (angle != zRot) {
        zRot = angle;
        emit zRotationChanged(angle);
        updateGL();
    }
}

void GLWidget::classSelected(const QString item)
{
	clsToDisplay = item.toShort();
}


void GLWidget::itrtSelected(const QString item)
{
	itrtToDisplay = item.toShort();
}

//! [6]
void GLWidget::initializeGL()
{
//GLfloat no_mat[] = {0.4, 0.4, 0.1, 1.0};
//GLfloat mat[] = {0.6, 0.8, 0.1, 1.0};
//GLfloat mat_ambient[] = {0.7, 0.7, 0.7, 1.0};
//GLfloat mat_ambient_color[] = {0.8, 0.8, 0.2, 1.0};
//GLfloat mat_diffuse[] = {0.1, 0.5, 0.8, 1.0};
//GLfloat mat_specular[] = {0.5, 0.7, 0.2, 1.0};
//GLfloat no_shininess[] = {0.0};
//GLfloat low_shininess[] = {5.0};
//GLfloat high_shininess[] = {100.0};
//GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
//
//glMaterialfv(GL_BACK, GL_AMBIENT, no_mat);
//glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse);
//glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
//glMaterialfv(GL_BACK, GL_SHININESS, low_shininess);
//
//glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
//glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
//glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
//glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
//
//GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
//GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
//GLfloat lmodel_ambient[] = {1.0, 1.0, 0.0, 1.0};
//glClearColor(0.0, 0.0, 0.0, 0.0);
//glShadeModel(GL_SMOOTH);
//glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
//glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
//
//glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glEnable(GL_DEPTH_TEST);
//
//		glEnable(GL_MULTISAMPLE);
    qglClearColor(trolltechGreen);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);

		if(deflt == disType) { object = makeObject(); }
}
//! [6]

//! [7]
void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslated(0, 0, -9.5);
    glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
    glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
    glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
		if(deflt != disType) { object = makeObject(); }
    glCallList(object);
}
//! [7]

//! [8]
void GLWidget::resizeGL(int width, int height)
{
    int side = qMin(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
}
//! [8]

//! [9]
void GLWidget::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
}
//! [9]

//! [10]
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    int dx = event->x() - lastPos.x();
    int dy = event->y() - lastPos.y();

    if (event->buttons() & Qt::LeftButton) {
        setXRotation(xRot + 8 * dy);
        setYRotation(yRot + 8 * dx);
    } else if (event->buttons() & Qt::RightButton) {
        setXRotation(xRot + 8 * dy);
        setZRotation(zRot + 8 * dx);
    }
    lastPos = event->pos();
}

void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
{
    qglColor(trolltechGreen.dark(250 + int(100 * x1)));

    glVertex3d(x1, y1, +0.05);
    glVertex3d(x2, y2, +0.05);
    glVertex3d(x2, y2, -0.05);
    glVertex3d(x1, y1, -0.05);
}

void GLWidget::normalizeAngle(int *angle)
{
    while (*angle < 0)
        *angle += 360 * 16;
    while (*angle > 360 * 16)
        *angle -= 360 * 16;
}

void GLWidget::drawFramework(double xmin, double xmax, 
														 double ymin, double ymax, 
														 double zmin, double zmax, 
														 double scale)
{
	xmin /= scale;
	xmax /= scale;

	ymin /= scale;
	ymax /= scale;
	
	if(zmax - zmin < 1e-3)
	{
		zmin = 0;
		zmax = xmax;
	}
	else
	{
		zmin /= scale;
		zmax /= scale;
	}

//1
	glColor3f(0, 0., 0.0);
	glVertex3f(xmin, ymin, zmin);
	glVertex3f(xmax, ymin, zmin);

	glColor3f(0., 0, 0.0);
	glVertex3f(xmin, ymin, zmin);
	glVertex3f(xmin, ymax, zmin);

	glColor3f(0., 0., 0.0);
	glVertex3f(xmin, ymin, zmin);
	glVertex3f(xmin, ymin, zmax);

	glColor3f((GLfloat)0.9, (GLfloat)0.2, (GLfloat)0.8);
//2
	glVertex3f(xmin, ymin, zmax);
	glVertex3f(xmax, ymin, zmax);

	glVertex3f(xmin, ymin, zmax);
	glVertex3f(xmin, ymax, zmax);
//3
	glVertex3f(xmin, ymax, zmax);
	glVertex3f(xmax, ymax, zmax);
//4
	glVertex3f(xmin, ymax, zmin);
	glVertex3f(xmin, ymax, zmax);

	glVertex3f(xmin, ymax, zmin);
	glVertex3f(xmax, ymax, zmin);
//5
	glVertex3f(xmax, ymax, zmin);
	glVertex3f(xmax, ymin, zmin);

	glVertex3f(xmax, ymax, zmin);
	glVertex3f(xmax, ymax, zmax);
//6
	glVertex3f(xmax, ymin, zmax);
	glVertex3f(xmax, ymax, zmax);

	glVertex3f(xmax, ymin, zmax);
	glVertex3f(xmax, ymin, zmin);
}

